//
//  MAActionView.m
//  meiang_edu
//
//  Created by iloveko8e on 2018/9/14.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAActionView.h"

@interface MAActionView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation MAActionView

- (instancetype)initWithImageName:(NSString *)imageName title:(NSString *)title {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ma_addTapEvent)];
        [self addGestureRecognizer:tap];
        
        self.imageView.image = [UIImage imageNamed:imageName];
        self.titleLabel.text = title;
        
        [self ma_addSubviews];
    }
    return self;
}

- (CGSize)intrinsicContentSize {
    CGSize size = [@"字" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]}];
    CGFloat height = 42 + size.height;
    return CGSizeMake(50, height);
}

- (void)updateImageName:(NSString *)imageName title:(NSString *)title {
    self.imageView.image = [UIImage imageNamed:imageName];
    self.titleLabel.text = title;
}

#pragma mark - private
- (void)ma_addSubviews {
    [self addSubview:self.imageView];
    [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.width.equalTo(@30);
        make.top.equalTo(@0);
        make.centerX.equalTo(self);
    }];

    [self addSubview:self.titleLabel];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(12);
        make.centerX.equalTo(self);
    }];
}

- (void)ma_addTapEvent {
    !self.clickBlock ?: self.clickBlock();
}

#pragma mark - getter
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLabel.font = [UIFont systemFontOfSize:10];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

@end
